package com.heatup.apt.management.service.accessdevice.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.heatup.apt.common.dao.AccessDeviceMapper;
import com.heatup.apt.management.service.accessdevice.AccessDeviceService;
import com.heatup.apt.model.AccessDevice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * AccessDevice 表数据服务层接口实现类
 */
@Service
public class AccessDeviceServiceImpl extends SuperServiceImpl<AccessDeviceMapper, AccessDevice> implements AccessDeviceService {

    @Autowired
    private AccessDeviceMapper accessDeviceMapper;

    /**
     * 分页查询门禁设备
     *
     * @param page          分页插件
     * @param accessDevice  查询参数
     * @return 分页数据
     */
    @Override
    public Page<AccessDevice> queryAccessDeviceListPage(Page<AccessDevice> page, AccessDevice accessDevice) {
        page.setRecords(accessDeviceMapper.queryAccessDeviceListPage(page,  accessDevice));
        return page;
    }

    /**
     * 保存门禁设备
     *
     * @param accessDevice 门禁设备
     * @return true 保存成功 / false 保存失败
     */
    @Override
    public boolean saveAccessDevice(AccessDevice accessDevice) {

        if (accessDevice.getPwdType() == 1) {
            accessDevice.setSuperPwd(null);
        } else {
            accessDevice.setDeviceSerialId(null);
        }
        if (accessDevice.getStoreLocation() == 1) {
            accessDevice.setWorkoutRoomNum(0);
            accessDevice.setWorkoutRoomName("大门");
        } else if (accessDevice.getStoreLocation() == 3) {
            accessDevice.setWorkoutRoomNum(null);
            accessDevice.setWorkoutRoomName("自助");
        }


        if (accessDevice.getStoreLocation() < 3 && accessDevice.getWorkoutRoomNum() == null)
            throw new RuntimeException("门禁操房位置编号不可为空");
        if (accessDevice.getStoreLocation() < 3 && StringUtils.isBlank(accessDevice.getWorkoutRoomName()))
            throw new RuntimeException("门禁操房名称不可为空");

        if (accessDevice.getPwdType() == 1 && StringUtils.isBlank(accessDevice.getDeviceSerialId()))
            throw new RuntimeException("动态密码设备 序列号不可为空");

        if (accessDevice.getPwdType() == 2 && StringUtils.isBlank(accessDevice.getSuperPwd()))
            throw new RuntimeException("固定密码设备 进店密码不可为空");


        AccessDevice accessDeviceQuery = new AccessDevice();
        accessDeviceQuery.setStoreId(accessDevice.getStoreId());
        accessDeviceQuery.setStoreLocation(accessDevice.getStoreLocation());
        if (accessDevice.getWorkoutRoomNum() != null) {
            accessDeviceQuery.setWorkoutRoomNum(accessDevice.getWorkoutRoomNum());
        }
        if (accessDevice.getId() != null) {
            accessDeviceQuery.setId(accessDevice.getId());
        }

        EntityWrapper<AccessDevice> entityWrapper = new EntityWrapper<>(accessDeviceQuery);
        List<AccessDevice> accessDeviceDBList = accessDeviceMapper.selectList(entityWrapper);

        accessDeviceDBList.forEach(accessDevice1 -> {

            if (accessDevice1 != null
                    && accessDevice1.getId() != null
                    && accessDevice.getId() != null
                    && !accessDevice1.getId().equals(accessDevice.getId())
                    && accessDevice1.getStoreId()!= null
                    && accessDevice.getStoreId()!=null
                    && accessDevice1.getStoreId().equals(accessDevice.getStoreId())
                    && accessDevice1.getWorkoutRoomNum() != null
                    && accessDevice.getWorkoutRoomNum() != null
                    && accessDevice1.getWorkoutRoomNum().equals(accessDevice.getWorkoutRoomNum()))
                throw new RuntimeException("设备同一门店同一位置不能重复");

            if (accessDevice1 != null
                    && accessDevice1.getId() != null
                    && accessDevice.getId() != null
                    && !accessDevice1.getId().equals(accessDevice.getId())
                    && accessDevice1.getStoreId()!= null
                    && accessDevice.getStoreId()!=null
                    && accessDevice1.getStoreId().equals(accessDevice.getStoreId())
                    && accessDevice.getStoreLocation() != null
                    && accessDevice1.getStoreLocation() != null
                    && accessDevice.getStoreLocation().equals(3)
                    && accessDevice.getStoreLocation().equals(accessDevice1.getStoreLocation()))
                throw new RuntimeException("一家门店目前只支持一个自助");

            if (StringUtils.isNotBlank(accessDevice.getDeviceSerialId())) {
                accessDevice1 = new AccessDevice();
                accessDevice1.setDeviceSerialId(accessDevice.getDeviceSerialId());
                AccessDevice accessDeviceCompare = accessDeviceMapper.selectOne(accessDevice1);

                if (accessDeviceCompare != null && accessDeviceCompare.getDeviceSerialId().equals(accessDevice.getDeviceSerialId()))
                    throw new RuntimeException("设备序列号不能重复");
            }

        });


        if (accessDevice.getId() == null) {
            accessDevice.setDeviceStatus(1);
            return accessDeviceMapper.insertSelective(accessDevice) > 0;
        } else {


            return accessDeviceMapper.updateById(accessDevice) > 0;
        }

    }

    /**
     * 查询所属门店的门禁列表
     *
     * @param storeId 门禁 ID
     * @return 所属门店的门禁列表
     */
    @Override
    public List<AccessDevice> queryAccessStoreWorkoutRoomList(Integer storeId) {
        AccessDevice accessDeviceQuery = new AccessDevice();
        accessDeviceQuery.setStoreId(storeId);
        accessDeviceQuery.setDeviceStatus(1);
        EntityWrapper<AccessDevice> entityWrapper = new EntityWrapper<>();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        entityWrapper.in("store_location",list);
        entityWrapper.setEntity(accessDeviceQuery);
        return accessDeviceMapper.selectList(entityWrapper);
    }

}